package jgjc.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jgjc.utils.ResultResUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class FramingInfoServiceController {

    @Resource
    private RestTemplate restTemplate;

    private String serviceUrl = "http://Basic-Data-Management";

    @GetMapping(value = "/consumer/bridge/bridgeSide/getBridgeList")
    public JSONObject getBridgeList(HttpServletRequest request) {
        int pageSize = Integer.valueOf(request.getParameter("pageSize"));
        int currentPage = Integer.valueOf(request.getParameter("currentPage"));
        JSONObject result = restTemplate.getForObject(serviceUrl + "/bridge/bridgeSide/getBridgeList?pageSize=" + pageSize + "&currentPage=" + currentPage, JSONObject.class);
        return result;
    }

    @GetMapping(value = "/consumer/bridge/bridgeSide/getSideTypeList")
    public JSONArray getSideTypeList() {
        JSONArray result = restTemplate.getForObject(serviceUrl + "/bridge/bridgeSide/getSideTypeList", JSONArray.class);
        return result;
    }

    @GetMapping(value = "/consumer/bridge/bridgeSide/getAccessLinearList")
    public JSONArray getAccessLinearList() {
        JSONArray result = restTemplate.getForObject(serviceUrl + "/bridge/bridgeSide/getAccessLinearList", JSONArray.class);
        return result;
    }

    @GetMapping(value = "/consumer/bridge/bridgeSide/getListByBridgeId")
    public JSONArray getListByBridgeId(HttpServletRequest request) {
        int bridgeId = Integer.parseInt(request.getParameter("bridgeId"));
        JSONArray result = restTemplate.getForObject(serviceUrl + "/bridge/bridgeSide/getListByBridgeId?bridgeId=" + bridgeId, JSONArray.class);
        return result;
    }


    @PostMapping(value = "/consumer/bridge/bridgeSide/add")
    public JSONObject add(@RequestBody Map<String, Object> map) {
        JSONObject jsonObject = restTemplate.postForObject(serviceUrl + "/bridge/bridgeSide/add", map, JSONObject.class);
        return jsonObject;
    }

    @DeleteMapping(value = "/consumer/bridge/bridgeSide/delete")
    public Map<String, Object> delete(@RequestBody Map<String, Object> map) {
        ResponseEntity<JSONObject> result = restTemplate.exchange(serviceUrl + "/bridge/bridgeSide/delete?id=" + map.get("id"), HttpMethod.DELETE, null, JSONObject.class);
        return result.getBody();
    }

    @PostMapping(value = "/consumer/bridge/bridgeSide/edit")
    public Map<String, Object> edit(@RequestBody Map<String, Object> map) {
        JSONObject jsonObject = restTemplate.postForObject(serviceUrl + "/bridge/bridgeSide/edit", map, JSONObject.class);
        return jsonObject;
    }

}
